ne=((1,None), (2,None), (3,1), (4,2), (5,None), (7,3), (9,3))
tree=[]

def get(n):
  for x in ne:
    if x[1]==n[0]:
      return x[0]

for x in ne:
  leaf=[]
  if x[1]==None:
    leaf.append(x[0])
    tree.append(leaf)
    l=xrange(len(tree))
for y in l:
  p=tree[y]
  print p
  # p.append(tree[y])
  leaf=[]
  ll=get(p)
  if ll<>None:
    leaf.append(ll)
  print leaf
  if len(leaf)>0:
    tree[y].append(leaf)
  # tree[y].append(p)
  
  
  
print tree